import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

public class JDBCDelete {
    public static void main(String[] args) throws SQLException {
        //第一步
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/attendance?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("123456");

        //第二步, 准备连接
        Connection connection = dataSource.getConnection();

        //第三步, 构造 sql 语句
        Scanner in = new Scanner(System.in);
        String sql = "delete from student where name = ?";
        System.out.println("请输入你要删除的名字");
        String name = in.next();

        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setString(1, name);

        //执行 sql 语句
        statement.executeUpdate();

        //关闭资源
        statement.close();
        connection.close();
    }
}
